package com.xuexiang.xuidemo.slice.button;

import com.xuexiang.xui_lib.component.button.CountDownButton;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.ToastUtil;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

/**
 * [SuperButton]API
 * SuperButton
 * .setShape(ShapeElement.OVAL) 设置形状
 * .setShapeCornersRadius(20)  设置圆角
 * .setShapeGradientColor(new RgbColor[]{new RgbColor(34, 197, 255), new RgbColor(255, 197, 34)}) 设置渐变颜色，从什么颜色-->什么颜色
 * .setShapeGradientOrientation(ShapeElement.Orientation.LEFT_TO_RIGHT)  渐变方向（只能代码设置）
 * .setShapeSelectorColor(new RgbColor(255, 106, 106)) 按下颜色
 * .setShapeColor(new RgbColor(0, 191, 255)) 默认颜色
 * .setShapeStrokeWidthColor(5, new RgbColor(34, 139, 34)); 边框宽度 ，颜色
 * .setShapeCornersRadius(float[]) 四个角分别设置园角
 * .setDisabledColor设置不可点击时颜色
 * .setShapeDashPathEffectValues 设置边框为虚线 以及颜色
 * <p>
 * [CountDownButton]API
 * .reset    重置
 * .setCount 设置总倒计时（必须）
 * .start    开始（必须）
 * .stop     暂停
 */
public class UnifiedButtonSlice extends AbilitySlice {

    private static final String END_LAB = "重新获取";
    private static final String END_LAB1 = "点击重试";
    private static final String END_LAB2 = "获取验证码";

    private static final String UNIT = "s";

    private CountDownButton button, button2, button3, button4;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_unified_slice_layout);

        findComponentById(ResourceTable.Id_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });

        button = (CountDownButton) findComponentById(ResourceTable.Id_code_btn);
        button2 = (CountDownButton) findComponentById(ResourceTable.Id_code_btn2);
        button3 = (CountDownButton) findComponentById(ResourceTable.Id_code_btn3);
        button4 = (CountDownButton) findComponentById(ResourceTable.Id_code_btn4);

        CountDownButton.CountDownButtonHelp.applyCountDownButton(button, 10, new CountDownButton.CountDownListener() {
            @Override
            public void downCount(CountDownButton button, int count) {
                button.setText(String.valueOf(count).concat(UNIT));
            }

            @Override
            public void finish(CountDownButton button) {
                button.setText(END_LAB);
            }
        });

        CountDownButton.CountDownButtonHelp.applyCountDownButton(button2, 20, new CountDownButton.CountDownListener() {
            @Override
            public void downCount(CountDownButton button, int count) {
                button.setText(String.valueOf(count).concat(UNIT));
            }

            @Override
            public void finish(CountDownButton button) {
                button.setText(END_LAB);
            }
        });

        CountDownButton.CountDownButtonHelp.applyCountDownButton(button3, 30, new CountDownButton.CountDownListener() {
            @Override
            public void downCount(CountDownButton button, int count) {
                button.setText("还剩:" + count + "秒");
            }

            @Override
            public void finish(CountDownButton button) {
                button.setText(END_LAB1);
                ToastUtil.showToast(getContext(), "时间到！");
            }
        });

        CountDownButton.CountDownButtonHelp.applyCountDownButton(button4, 60, new CountDownButton.CountDownListener() {
            @Override
            public void downCount(CountDownButton button, int count) {
                button.setText(String.valueOf(count).concat(UNIT));
            }

            @Override
            public void finish(CountDownButton button) {
                button.setText(END_LAB2);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        button.reset();
        button2.reset();
        button3.reset();
        button4.reset();

        button.setText("获取验证码");
        button2.setText("获取验证码");
        button3.setText("获取验证码");
        button4.setText("获取验证码");
    }
}
